#!/bin/bash

# 部署脚本示例 - 展示如何在部署期间控制进程监控

set -e

# 配置
HEALTH_CHECK_BIN="./health-check"
CONFIG_FILE="configs/config.yaml"
PROCESS_NAME="DEMO"
PROCESS_GROUP="DEMO_GROUP"

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 日志函数
log_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

log_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查健康检查程序是否存在
check_health_check() {
    if [ ! -f "$HEALTH_CHECK_BIN" ]; then
        log_error "健康检查程序不存在: $HEALTH_CHECK_BIN"
        exit 1
    fi
}

# 禁用进程监控
disable_monitoring() {
    log_info "禁用进程监控..."
    
    # 禁用单个进程
    if [ -n "$PROCESS_NAME" ]; then
        log_info "禁用进程: $PROCESS_NAME"
        $HEALTH_CHECK_BIN -disable-process "$PROCESS_NAME" -config "$CONFIG_FILE"
    fi
    
    # 禁用进程组
    if [ -n "$PROCESS_GROUP" ]; then
        log_info "禁用进程组: $PROCESS_GROUP"
        $HEALTH_CHECK_BIN -disable-group "$PROCESS_GROUP" -config "$CONFIG_FILE"
    fi
    
    log_info "进程监控已禁用"
}

# 启用进程监控
enable_monitoring() {
    log_info "启用进程监控..."
    
    # 启用单个进程
    if [ -n "$PROCESS_NAME" ]; then
        log_info "启用进程: $PROCESS_NAME"
        $HEALTH_CHECK_BIN -enable-process "$PROCESS_NAME" -config "$CONFIG_FILE"
    fi
    
    # 启用进程组
    if [ -n "$PROCESS_GROUP" ]; then
        log_info "启用进程组: $PROCESS_GROUP"
        $HEALTH_CHECK_BIN -enable-group "$PROCESS_GROUP" -config "$CONFIG_FILE"
    fi
    
    log_info "进程监控已启用"
}

# 检查进程状态
check_status() {
    log_info "检查进程状态..."
    
    if [ -n "$PROCESS_NAME" ]; then
        log_info "进程 $PROCESS_NAME 状态:"
        $HEALTH_CHECK_BIN -name "$PROCESS_NAME" -config "$CONFIG_FILE"
    fi
    
    if [ -n "$PROCESS_GROUP" ]; then
        log_info "进程组 $PROCESS_GROUP 状态:"
        $HEALTH_CHECK_BIN -group-status "$PROCESS_GROUP" -config "$CONFIG_FILE"
    fi
}

# 部署前准备
pre_deploy() {
    log_info "开始部署前准备..."
    
    # 检查健康检查程序
    check_health_check
    
    # 禁用监控
    disable_monitoring
    
    # 等待一段时间确保监控已停止
    sleep 2
    
    log_info "部署前准备完成"
}

# 部署后恢复
post_deploy() {
    log_info "开始部署后恢复..."
    
    # 启用监控
    enable_monitoring
    
    # 等待一段时间确保监控已启动
    sleep 2
    
    # 检查状态
    check_status
    
    log_info "部署后恢复完成"
}

# 显示帮助信息
show_help() {
    echo "部署脚本 - 控制进程监控"
    echo ""
    echo "用法: $0 [选项]"
    echo ""
    echo "选项:"
    echo "  pre-deploy    部署前准备（禁用监控）"
    echo "  post-deploy   部署后恢复（启用监控）"
    echo "  disable       禁用监控"
    echo "  enable        启用监控"
    echo "  status        检查状态"
    echo "  help          显示帮助信息"
    echo ""
    echo "示例:"
    echo "  $0 pre-deploy    # 部署前禁用监控"
    echo "  $0 post-deploy   # 部署后启用监控"
    echo "  $0 disable       # 手动禁用监控"
    echo "  $0 enable        # 手动启用监控"
}

# 主函数
main() {
    case "${1:-help}" in
        "pre-deploy")
            pre_deploy
            ;;
        "post-deploy")
            post_deploy
            ;;
        "disable")
            disable_monitoring
            ;;
        "enable")
            enable_monitoring
            ;;
        "status")
            check_status
            ;;
        "help"|"-h"|"--help")
            show_help
            ;;
        *)
            log_error "未知选项: $1"
            show_help
            exit 1
            ;;
    esac
}

# 执行主函数
main "$@"
